package com.chandra.pos.employee.service.impl;

/* @(#) EmployeeDaoimpl.java
 * Copyright 2013 - 2015 QatarAirways Group. All Rights Reserved.
 *
 * This Software is the proprietary information of QR Group
 * Use is subject to License terms.
 * Created by T4671 on 18/03/2015.
 */


import com.chandra.pos.employee.dao.EmployeeRepository;
import com.chandra.pos.employee.dao.entity.EmployeeEntity;
import com.chandra.pos.employee.model.EmployeeFilter;
import com.chandra.pos.employee.model.EmployeeList;
import com.chandra.pos.employee.model.EmployeeModel;
import com.chandra.pos.employee.service.EmployeeRole;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.persistence.EntityExistsException;
import javax.persistence.EntityNotFoundException;
import java.util.ArrayList;
import java.util.List;

@Component
public class EmployeeRoleImpl implements EmployeeRole {

    @Autowired
    EmployeeRepository employeeRepository;

    @Override
    public EmployeeModel saveEmployee(EmployeeModel employeeModel) throws EntityExistsException {


        EmployeeEntity employeeEntity = employeeRepository.findByUserName(employeeModel.getUserName());

        if (employeeEntity != null && !employeeEntity.getEmployeeId().equals(employeeModel.getEmployeeId()))

            throw new EntityExistsException("Employee with username:" + employeeModel.getUserName() + "already exist");

        employeeEntity = new EmployeeEntity();

        BeanUtils.copyProperties(employeeModel, employeeEntity);
        employeeEntity = employeeRepository.save(employeeEntity);

        employeeModel.setEmployeeId(employeeEntity.getEmployeeId());

        return employeeModel;
    }

    @Override
    public EmployeeModel updateEmployee(EmployeeModel employeeModel) {

        EmployeeEntity employeeEntity = new EmployeeEntity();

        BeanUtils.copyProperties(employeeModel, employeeEntity);
        employeeEntity = employeeRepository.save(employeeEntity);


        return employeeModel;
    }

    @Override
    public EmployeeList searchEmployee(EmployeeFilter employeeFilter) {
        EmployeeList employeeList = new EmployeeList();

        List<EmployeeModel> employeeModels = new ArrayList<>();
        Page<EmployeeEntity> employeeEntities = null;

        PageRequest request =
                new PageRequest(employeeFilter.getPage() - 1, employeeFilter.getRows(), employeeFilter.getDBSord(), employeeFilter.getSidx());

        if (!StringUtils.isEmpty(employeeFilter.getQuery()))

            employeeEntities = employeeRepository.searchByName(employeeFilter.getQuery(), request);
        else
            employeeEntities = employeeRepository.findAll(request);


        for (EmployeeEntity customerEntity : employeeEntities.getContent()) {
            EmployeeModel customerModel = new EmployeeModel();

            BeanUtils.copyProperties(customerEntity, customerModel);

            employeeModels.add(customerModel);
        }
        employeeList.setPage(employeeFilter.getPage());
        employeeList.setRecords(employeeEntities.getTotalElements());
        employeeList.setTotal(employeeEntities.getTotalPages());

        employeeList.setEmployees(employeeModels);
        return employeeList;
    }

    @Override
    public EmployeeModel getByUserName(String userName) throws EntityNotFoundException {


        EmployeeEntity employeeEntity = employeeRepository.findByUserName(userName);

        if (employeeEntity == null)
            throw new EntityNotFoundException("User with name " + userName + " not found");
        EmployeeModel employeeModel = new EmployeeModel();

        BeanUtils.copyProperties(employeeEntity, employeeModel);

        return employeeModel;
    }

    @Override
    public EmployeeModel getEmployeeDetail(EmployeeFilter filter) throws EntityNotFoundException {

       if(StringUtils.isNotEmpty(filter.getUserName()))
           return getByUserName(filter.getUserName());

        else
       {
           EmployeeEntity employeeEntity=employeeRepository.findOne(filter.getEmployeeId());
           if (employeeEntity == null)
               throw new EntityNotFoundException("User with name " + filter.getEmployeeId() + " not found");
           EmployeeModel employeeModel = new EmployeeModel();

           BeanUtils.copyProperties(employeeEntity, employeeModel);
           return employeeModel;

       }

    }
}
